package com.luyu.hystrixdemo;

import com.microsoft.playwright.*;
import com.microsoft.playwright.options.LoadState;

import java.nio.file.Paths;

public class PlaywrightExample {
    public static void main(String[] args) throws InterruptedException {
        try (Playwright playwright = Playwright.create()) {
            // 指定本地浏览器的路径
            String browserPath = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";


            // 指定用户数据目录（加载本地插件）
            String userDataDir = "C:\\Users\\HASEE\\AppData\\Local\\Google\\Chrome\\User Data"; // 修改为你的用户数据目录
            BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions()
                    // .setArgs(List.of("--user-data-dir=" + userDataDir))
                    ;
            // 启动本地浏览器
            Browser browser = playwright.chromium().launch(
                    launchOptions
                    .setExecutablePath(Paths.get(browserPath)) // 设置浏览器路径
                    // 设置为 false 以显示浏览器窗口
                    .setHeadless(false))
                    ;


            BrowserContext context = browser.newContext();
            Page page = context.newPage();


            // 打开百度
            page.navigate("https://app.nexus.xyz/");
            // 等待页面加载完成
            page.waitForLoadState(LoadState.NETWORKIDLE);

            // 获取页面视口大小
            int viewportWidth = page.viewportSize().width;
            int viewportHeight = page.viewportSize().height;
            page.setViewportSize(1080,1080);
            // 等待五秒
            // page.waitForTimeout(10000);
            // page.getByAltText("Sign up to earn NEX").click();
            // // 移动鼠标到页面中心
            // page.mouse().move(page.viewportSize().width / 2, page.viewportSize().height / 2);

            // 移动鼠标到页面中心
            page.mouse().move(viewportWidth / 2, viewportHeight / 2);


            // 点击开启
            page.locator("xpath=/html/body/div[3]/div[2]/main/main/div[2]/div/div/div[1]/div[1]/div/div/div/div/div[2]").click();

            // 等待 2 秒
            page.waitForTimeout(2000);

            // 移动鼠标到页面右上角
            page.mouse().move(viewportWidth - 10, 0);
            // 关闭浏览器
            // browser.close();
            Thread.sleep(10000000);
        }
    }
}
